#!/bin/bash

# RX教育平台部署脚本
# 使用方法: ./deploy.sh [环境] [操作]
# 环境: dev|staging|prod
# 操作: build|deploy|restart|logs|stop

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查参数
if [ $# -lt 2 ]; then
    log_error "使用方法: $0 [环境] [操作]"
    log_info "环境: dev|staging|prod"
    log_info "操作: build|deploy|restart|logs|stop|status"
    exit 1
fi

ENVIRONMENT=$1
ACTION=$2

# 验证环境参数
if [[ ! "$ENVIRONMENT" =~ ^(dev|staging|prod)$ ]]; then
    log_error "无效的环境参数: $ENVIRONMENT"
    log_info "支持的环境: dev, staging, prod"
    exit 1
fi

# 验证操作参数
if [[ ! "$ACTION" =~ ^(build|deploy|restart|logs|stop|status)$ ]]; then
    log_error "无效的操作参数: $ACTION"
    log_info "支持的操作: build, deploy, restart, logs, stop, status"
    exit 1
fi

# 设置环境变量
case $ENVIRONMENT in
    "dev")
        COMPOSE_FILE="docker-compose.yml"
        PROJECT_NAME="rx-education-dev"
        ;;
    "staging")
        COMPOSE_FILE="docker-compose.staging.yml"
        PROJECT_NAME="rx-education-staging"
        ;;
    "prod")
        COMPOSE_FILE="docker-compose.prod.yml"
        PROJECT_NAME="rx-education-prod"
        ;;
esac

# 检查Docker和Docker Compose
check_dependencies() {
    log_info "检查依赖..."
    
    if ! command -v docker &> /dev/null; then
        log_error "Docker 未安装或不在PATH中"
        exit 1
    fi
    
    if ! command -v docker-compose &> /dev/null; then
        log_error "Docker Compose 未安装或不在PATH中"
        exit 1
    fi
    
    log_success "依赖检查通过"
}

# 构建镜像
build_images() {
    log_info "构建 $ENVIRONMENT 环境的Docker镜像..."
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME build --no-cache
    
    log_success "镜像构建完成"
}

# 部署服务
deploy_services() {
    log_info "部署 $ENVIRONMENT 环境的服务..."
    
    # 创建网络（如果不存在）
    docker network create rx-network 2>/dev/null || true
    
    # 启动服务
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME up -d
    
    # 等待服务启动
    log_info "等待服务启动..."
    sleep 10
    
    # 检查服务状态
    check_services_health
    
    log_success "服务部署完成"
}

# 重启服务
restart_services() {
    log_info "重启 $ENVIRONMENT 环境的服务..."
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME restart
    
    log_success "服务重启完成"
}

# 停止服务
stop_services() {
    log_info "停止 $ENVIRONMENT 环境的服务..."
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME down
    
    log_success "服务已停止"
}

# 查看日志
view_logs() {
    log_info "查看 $ENVIRONMENT 环境的服务日志..."
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME logs -f
}

# 检查服务状态
check_status() {
    log_info "检查 $ENVIRONMENT 环境的服务状态..."
    
    docker-compose -f $COMPOSE_FILE -p $PROJECT_NAME ps
}

# 检查服务健康状态
check_services_health() {
    log_info "检查服务健康状态..."
    
    # 检查后端API
    if curl -f http://localhost:3000/health &>/dev/null; then
        log_success "后端API服务正常"
    else
        log_warning "后端API服务可能未就绪"
    fi
    
    # 检查前端服务
    if curl -f http://localhost:3001 &>/dev/null; then
        log_success "管理后台服务正常"
    else
        log_warning "管理后台服务可能未就绪"
    fi
    
    if curl -f http://localhost:3002 &>/dev/null; then
        log_success "学生端服务正常"
    else
        log_warning "学生端服务可能未就绪"
    fi
    
    if curl -f http://localhost:3003 &>/dev/null; then
        log_success "移动端服务正常"
    else
        log_warning "移动端服务可能未就绪"
    fi
}

# 主逻辑
main() {
    log_info "开始执行 $ENVIRONMENT 环境的 $ACTION 操作..."
    
    check_dependencies
    
    case $ACTION in
        "build")
            build_images
            ;;
        "deploy")
            build_images
            deploy_services
            ;;
        "restart")
            restart_services
            ;;
        "logs")
            view_logs
            ;;
        "stop")
            stop_services
            ;;
        "status")
            check_status
            ;;
    esac
    
    log_success "操作完成!"
}

# 执行主函数
main